package gwtgaetools.client.ui;

import gwtgaetools.client.ItemManager;
import gwtgaetools.client.listeners.Listener;

import com.google.gwt.user.client.ui.TextBox;

public class SmartInlineTextbox<T> extends TextBox implements Listener<ItemManager<T>> {

	public SmartInlineTextbox() {
		addStyleName("inline");
	}
	
	public SmartInlineTextbox(ItemManager<T> m) {
		this();
		m.listeners.add(this);
		observe(m);
	}
	
	@Override
	public void observe(ItemManager<T> t) {
		if (t.getItem()!=null) {
			int pos = getCursorPos();
			setText(t.getItem().toString());
			setCursorPos(Math.min(pos, t.getItem().toString().length()));
		} else {
			setText("");
		}
	}

}
